﻿<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';

header('Content-Type: text/plain');


//$action = MiscUtils::getParam('name', NULL);

$q = json_decode(MiscUtils::getParam('q', NULL));

   foreach($q as &$tmp)
   {
     //print_r($tmp);

        if($tmp->n)
        {//tablename

			//delete
			
			if(!empty($tmp->r))
			{
				foreach($tmp->r as &$obj)
				{
					/*echo"********obj***********************";
					echo "\n";
					print_r($obj);
					echo "\n";
                    echo"********obj***********************";
                    echo "\n";	*/				
					$orm1 = 'produceevent';
					$orm2 = 'producetrack';
					$orm21 = 'producetrackuser';
					$orm3 = 'producepurchaseitem';
					$orm31 = 'producepurchaseitemselect';
					$orm4 = 'producenote';
					$orm5 = 'produceuser';
					$orm6 = 'produceselect';
					
					if(isset($obj->id) || isset($obj->e_oid))
					{
						/*$result1 = producefind($orm1,"d.produce_trackId='$obj->trackId'");
						foreach($result1->data as &$event) 
						{
						    produceremove($orm1, $event);
						}*/
						produceremove($orm1,'produce_trackId',$obj->trackId);

					  /*$result2 = producefind($orm2,"d.produce_id=$obj->id", NULL);
					  foreach($result2->data as &$track) {
						$result21 = producefind($orm21,"d.track_trackId='$track->trackId'", NULL);
						foreach($result21->data as &$tu) {
						  produceremove($orm21, $tu);
						}
						produceremove($orm2, $track);
					  }*/
						
						$sqlorm2 = "select * from ns_".$orm2." where produce_id = '".$obj->id."' ";
						$orm2arr = getall($sqlorm2);
						
						if(!empty($orm2arr))
						{
							foreach($orm2arr as &$track)
							{
								produceremove($orm21,'track_trackId',$track->trackId);
							}
							
							produceremove($orm2,'produce_id',$obj->id);
						}
						
						
					  
					  /*$result3 = producefind($orm3,"d.produce_trackId='$obj->trackId'", NULL);
					  foreach($result3->data as &$pi) {
						$result31 = producefind($orm31,"d.purchase_trackId='$pi->trackId'", NULL);
						foreach($result31->data as &$sel) {
						  produceremove($orm31, $sel);
						}
						produceremove($orm3, $pi);
					  }*/
						
						$sqlorm3 = "select * from ns_".$orm3."  where produce_trackId = '".$obj->trackId."'";
						$orm3arr = getall($sqlorm3);
						
						if(!empty($orm3arr))
						{
							foreach($orm3arr as &$pi)
							{
								produceremove($orm31,'purchase_trackId',$pi->trackId);
							}
							produceremove($orm3,'produce_trackId',$obj->trackId);
						}
					  
					  
					  /*$result4 = producefind($orm4,"d.produce_trackId='$obj->trackId'", NULL);
					  foreach($result4->data as &$note) {
						produceremove($orm4, $note);$obj->produce_trackId
					  }*/
					  produceremove($orm4,'produce_trackId',$obj->trackId);
					  
					  
					  /*$result5 = producefind($orm5, "d.produce_id=$obj->id", NULL);
					  foreach($result5->data as &$user) {
						produceremove($orm5, $user);
					  }*/
					  
					  produceremove($orm5,'produce_id',$obj->id);
					  
					  
					  /*$result6 = producefind($orm6,"d.produce_trackId='$obj->trackId'", NULL);
					  foreach($result6->data as &$select) {
						produceremove($orm6, $select);
					  }*/
					  
					  produceremove($orm6,'produce_trackId',$obj->trackId);
					                           						
					
					
					}
					
					produceremove($tmp->n,'e_oid',$obj->e_oid);
				
				}			
			
			}//$tmp->r not null

			
			
            foreach($tmp->a as &$obj)
            {
                if(isset($obj->id) || isset($obj->e_oid))
                {//update
                    produceupdate($obj,$tmp->n);         
                }else{//add
                    produceadd($obj,$tmp->n);      
                    
                }                   
            }			
			
		}//not null     		
   
   }

/*************数据操作************/ 

    function produceadd($obj,$tablename,$debug='')
	{
        if(!empty($debug))
        {
        	echo "******************tablename:".$tablename."*********************";
		    echo "\n";
        	print_r($obj);
		    echo "***************************************";
		    echo "\n";            
		}

        if(!empty($obj))
        {   
		    $producefield = '';
			$producevalue = '';
			
		    foreach($obj as $key=>$value)
            {
            	
            	switch ($tablename)
            	{
            		case 'producepurchaseitem':
            			{
            				$nooperate_array = array('pi_selects','v2_pUsers','v2_purchaseItems','v2_events','v2_tracks',
                                                     'v2_produces','v2_notes','_exist','when','pUsers','selects','notes','_added',
                                                     'v2_selects','v2_selects','selects','notes','_added','cancelled','isFree','urgent',
                                                     'order_id');
            			}
            			break;

            		case 'producepurchaseitemselect':
            			{
            				$nooperate_array = array('pi_selects','v2_pUsers','v2_purchaseItems','v2_events','v2_tracks',
                                                     'v2_produces','v2_notes','_exist','when','pUsers','selects','notes','_added',
                                                     'v2_selects','v2_selects','selects','notes','_added','cancelled','isFree','urgent',
                                                     'order_id','purchaseitem_id');
            			}	
            			break;	
            			
            		default:
            			{
                            $nooperate_array = array('pi_selects','v2_pUsers','v2_purchaseItems','v2_events','v2_tracks',
                                        'v2_produces','v2_notes','_exist','when','pUsers','selects','notes','_added','v2_selects');            				
            			}
            			
            			break;	
            	}
            	
			    if(!in_array($key,$nooperate_array))
                {
                	
                	//if( == 'producepurchaseitem')
				    $producefield .='`'.$key.'`,';

                    if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date','isExpired')))
                    {
                    	if(is_object($obj->$key))
                    	{
                    		$value = SimpleDate::toStamp($value);
                    		
                    		if(empty($value)) $value = 0;
                    	}else{
                    		if(empty($obj->$key)) $value = 0;
                    	}
                    }
                        
					$producevalue .= " '".addslashes($value)."',";     					
				
				}

                if(in_array($key,array('when')))
				{
				    $producefield .='`when`,'; 
					
					if(empty($obj->when)) $obj->when = 0;
					
					$producevalue .= " '".$obj->when."',";
				}  				
			
			
			}

            $producefield = substr($producefield,0,-1);
            $producevalue = substr($producevalue,0,-1);

			$producesql = "insert into ns_".$tablename."(`e_oid`,".$producefield.") values(NULL,".$producevalue.")";  
			 
            if(!empty($debug))
            {
			    echo $producesql."\n";         
			}else{
			    sql_execute($producesql);
			} 
			
			unset($key);
			unset($value);
			
			/*if(isset($obj->pi_selects))
			{
				if(!empty($obj->pi_selects))
				{
					pi_selects_operate($obj->pi_selects,$debug);
					
				}//not null
				
			}// exist pi_selects*/
			
			/*if(isset($obj->v2_pUsers))
			{
				if(!empty($obj->v2_pUsers))
				{
					pusers_operate($obj->v2_pUsers,$debug);
					
				}//not null
				
			}// exist v2_pUsers*/
			
			/*if(isset($obj->v2_purchaseItems))
			{
				if(!empty($obj->v2_purchaseItems))
				{
					v2_purchaseItems_operate($obj->v2_purchaseItems,$debug);
				}//not null
				
			}//exist v2_purchaseItems*/
			
 		/**
 		 * v2_events
 		 */
        /*if(isset($obj->v2_events))
        {
        	if(!empty($obj->v2_events))
        	{
        		v2_events_operate($obj->v2_events,$debug);
        	}
        	
        }//是否存在v2_events ，end v2_events operate*/


        /*if(isset($obj->v2_tracks))
        {
        	if(!empty($obj->v2_tracks))
        	{
        		v2_tracks_operate($obj->v2_tracks,$debug);
        	}
        	
        }//exist  v2_tracks ,end v2_tracks operate*/

       /* if(isset($obj->v2_notes))
        {
        	if(!empty($obj->v2_notes))
        	{
        		v2_notes_operate($obj->v2_notes,$debug);
        		
        	}//v2_notes not null
        	
        	
        }//exist notes note null*/

//        if(isset($obj->v2_produces))
//        {
//        	if(!empty($obj->v2_produces))
//        	{
//        		foreach ($obj->v2_produces as $v2produce)
//        		{
//        			if(isset($v2produce->id) || isset($v2produce->e_oid))
//        			{//update 
//        				$v2producesql = 'update ns_produce set ';
        				
//        				foreach($v2produce as $key => $value)
//        				{
//        				    if(!in_array($key,array('pi_selects','v2_purchaseItems','pUsers','v2_events','v2_tracks','v2_produces','v2_notes','id','e_oid','_text','when','deadline')))
//                            {
//                               if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date'))) $value = SimpleDate::toStamp($value);
//                               $v2producesql .= '`'.$key."`='".addslashes($value)."',";
//                            }  

//	                       if(in_array($key,array('when','deadline')))
//	                       {
//	                           if(empty($v2produce->$key))
//	                           {
//	                           	  $v2produce->$key = 0;
//	                           	  $v2producesql .= " `$key` = '".$v2produce->$key."',";
//	                           }else{
//	                               $v2producesql .= " `$key` = '".SimileDate::toStamp($v2produce->$key)."',";	
//	                           }
	                                
	                           
//	                       }                             
        					
//        				}
        				
        			

          				
        				
//                        $v2producesql =substr($v2producesql,0,-1);
                        
//                        $v2producesql .= " where e_oid = '".$v2produce->e_oid."'";
                        
//                        if(!empty($debug))
//                        {
//                            echo $v2producesql."\n";
//                        }else{
//                            sql_execute($v2producesql);
//                        }
//                        unset($key);
//                        unset($value);

                        /**
                         * pi_selects
                         */
//                        if(isset($v2produce->pi_selects))
//                        {
//	                        if(!empty($v2produce->pi_selects))
//	                        {
//	                        	pi_selects_operate($v2produce->pi_selects,$debug);
//	                        }
//                        }
                        
                        /**
                         * v2_purchaseItems
                         */
//                        if(isset($v2produce->v2_purchaseItems))
//                        {
//                        	if(!empty($v2produce->v2_purchaseItems))
//                        	{
//                        		v2_purchaseItems_operate($v2produce->v2_purchaseItems,$debug);
//                        	}
//                        }
                        
        				
//        			}else{//add
//			            $v2producefield = '';
//			            $v2producevalue = '';
			            
//			            foreach($obj->v2_produces as $key => $value)
//			            {
//			                if(!in_array($key,array('pi_selects','v2_pUsers','v2_purchaseItems','v2_events','v2_tracks','v2_produces','v2_notes','_exist','when','deadline')))
//			                {
//			                    $v2producefield .='`'.$key.'`,';
			
//			                    if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date'))) $value = SimpleDate::toStamp($value);
			                        
//			                    $v2producevalue .= " '".addslashes($value)."',";                      
			                
//			                }
			
//			                if(in_array($key,array('when','deadline')))
//			                {
//			                    $v2producefield .='`$key`,'; 
			                    
//			                    if(empty($obj->$key))
//			                    {
//			                    	$obj->$key = 0;
//			                    	$v2producevalue .= " '".$obj->$key."',";
			                    	
//			                    }else{
//			                        $v2producevalue .= " '".SimpleDate::toStamp($obj->$key)."',";	
//			                    }
			                    
			                    
//			                }               
			            
			            
//			            }
			
//			            $v2producefield = substr($v2producefield,0,-1);
//			            $v2producevalue = substr($v2producevalue,0,-1);
			
//			            $v2producesql = "insert into ns_produce(`e_oid`,".$v2producefield.") values(NULL,".$v2producevalue.")";  
			             
//			            if(!empty($debug))
//			            {
//			                echo $v2producesql."\n";         
//			            }else{
//			                sql_execute($v2producesql);
//			            } 
			            
//			            unset($key);
//			            unset($value);

			            /**
			             * pi_selects
			             */
        			                        /**
                         * pi_selects
                         */
//                        if(isset($v2produce->pi_selects))
//                        {
//                            if(!empty($v2produce->pi_selects))
//                            {
//                                pi_selects_operate($v2produce->pi_selects,$debug);
//                            }
//                        }
                        
                        /**
                         * v2_purchaseItems
                         */
//                        if(isset($v2produce->v2_purchaseItems))
//                        {
//                            if(!empty($v2produce->v2_purchaseItems))
//                            {
//                              v2_purchaseItems_operate($v2produce->v2_purchaseItems,$debug);
//                            }
//                        }
        				
//        			}//end add
//        		}
//        	}
        	
        	
//        }//exist v2_produces ,end v2_produces operate        
			
		
		}//obj not null        		
	
	}  
   
/**
 *
 * pdoupdate 
 */    
 function produceupdate($obj,$tablename,$debug='')
 {
 	if(!empty($debug))
 	{
 		echo "**************tablename:".$tablename."**************";
 		print_r($obj);
 	}
 	
 	if (!empty($obj))
 	{
 		
 		$producefield = "update ns_".$tablename." set ";
 		
 		foreach ($obj as $key => $value)
 		{
 			if(!in_array($key,array('pi_selects','v2_purchaseItems','pUsers','v2_events','v2_tracks','v2_produces','v2_notes',
 			                        'id','e_oid','_text','when','deadline','_exist','selects','notes','v2_selects')))
 			{
 				if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date')))
 				{
 					if(empty($obj->$key)) $value = 0;
 					
 					if(is_object($obj->$key))
 					{
 					     $value = SimpleDate::toStamp($value);
 					}
 				}
 				
 			    $producefield .= '`'.$key."`='".addslashes($value)."',";
 			}
 			

           if(in_array($key,array('when','deadline')))
           {
               if(empty($obj->$key)) 
               {
               	  $obj->$key = 0;
                  $producefield .= " `$key` = '".$obj->$key."',";
               }else{
               	  $value = SimpleDate::toStamp($obj->$key);
               	  if(empty($value)) $value = 0;
               	  
               	  $producefield .= " `$key` = '".$value."',";
               }
                    
               
           }        			
 			
 		}
 		
 		$producefield = substr($producefield,0,-1);
 		$producefield .=" where e_oid = '".$obj->id."'";
 		
 		if(!empty($debug))
 		{
 			echo $producefield."\n";
 		}else{
 			sql_execute($producefield);
 		}
 		
 		/**
 		 * pi_selects operate
 		 * 
 		 */
 		if(isset($obj->pi_selects))
 		{
 			if(!empty($obj->pi_selects))
 			{
               pi_selects_operate($obj->pi_selects,$debug);
 				
 			}//not null
 			
 		}//是否存在pi_selects
 		
 		/**
 		 * v2_purchaseItems operate
 		 */
 		if(isset($obj->v2_purchaseItems))
 		{

             
                $original_producepurchaseitem_id = getsonid('ns_producepurchaseitem','e_oid','produce_trackId',$obj->trackId);//原来的ns_producepurchaseitem数组
                 
                if(!empty($debug))
                {
                   print_r($original_producepurchaseitem_id); 
                }       
             
               $new_producepurchaseitem_id = array();
               
               foreach($obj->v2_purchaseItems as $ppurchaseitem1)
               {
                    $new_producepurchaseitem_id[] = $ppurchaseitem1->e_oid;
               }

               $delete_ppurchaseitem_id = array_diff($original_producepurchaseitem_id, $new_producepurchaseitem_id);//需要delete 的packageitem_id
               
               if(!empty($delete_ppurchaseitem_id))
               {
                    foreach($delete_ppurchaseitem_id as $deleteid)
                    {
                    	$original_producepurchaseitemselect_id = getsonid('ns_producepurchaseitemselect','trackId','e_oid',$deleteid);
                    	
                    	if(!empty($debug))
                    	{
                    		print_r($original_producepurchaseitemselect_id);
                    	}
                    	
                        if(!empty($original_producepurchaseitemselect_id))
                        {
                        	$ppurchaseitemselectsql = "delete from ns_producepurchaseitemselect where purchase_trackId = '".$original_producepurchaseitemselect_id[0]."'"; 
                        	sql_execute($ppurchaseitemselectsql);
                        }
                            
                        $ppurchaseitemsql = "delete from ns_producepurchaseitem where e_oid = '".$deleteid."' ";
                        sql_execute($ppurchaseitemsql);                       
                    
                    }
                }//delete packitems end      
                 			
 			
 			if(!empty($obj->v2_purchaseItems))
 			{
 				//v2_purchaseItems_operate($obj->v2_purchaseItems,$debug);
 			}
 			
 		}//是否存在v2_purchaseItems  end v2_purchaseItems operate

 		if(isset($obj->v2_pUsers))
 		{
 			if(!empty($obj->v2_pUsers))
 			{
 				pusers_operate($obj->v2_pUsers,$debug);
 				
 			}//$obj->pUsers not null
 			
 		}//是否存在pUsers，  end pUsers operate
 		
 		/**
 		 * v2_events
 		 */
        if(isset($obj->v2_events))
        {
        	if(!empty($obj->v2_events))
        	{
        		v2_events_operate($obj->v2_events,$debug);
        	}
        	
        }//是否存在v2_events ，end v2_events operate

        if(isset($obj->v2_tracks))
        {
        	if(!empty($obj->v2_tracks))
        	{
        		v2_tracks_operate($obj->v2_tracks,$debug);
        	}
        	
        }//exist  v2_tracks ,end v2_tracks operate
        
        if(isset($obj->v2_notes))
        {
        	if(!empty($obj->v2_notes))
        	{
        		v2_notes_operate($obj->v2_notes,$debug);
        		
        	}//v2_notes not null
        	
        	
        }//exist notes note null
        
        if(isset($obj->v2_produces))
        {
        	if(!empty($obj->v2_produces))
        	{
        		foreach ($obj->v2_produces as $v2produce)
        		{
        			if(isset($v2produce->id) || isset($v2produce->e_oid))
        			{//update 
        				$v2producesql = 'update ns_produce set ';
        				
        				foreach($v2produce as $key => $value)
        				{
        				    if(!in_array($key,array('pi_selects','v2_purchaseItems','pUsers','v2_events','v2_tracks','v2_produces','v2_notes','id','e_oid','_text','when','deadline')))
                            {
                               if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date'))) $value = SimpleDate::toStamp($value);
                               $v2producesql .= '`'.$key."`='".addslashes($value)."',";
                            }  

	                       if(in_array($key,array('when','deadline')))
	                       {
	                           if(empty($v2produce->$key))
	                           {
	                           	  $v2produce->$key = 0;
	                           	  $v2producesql .= " `$key` = '".$v2produce->$key."',";
	                           }else{
	                               $v2producesql .= " `$key` = '".SimileDate::toStamp($v2produce->$key)."',";	
	                           }
	                                
	                           
	                       }                             
        					
        				}
        				
        				
		                /*if(in_array($key,array('when')))
		                {
		                    $v2producesql .='`when`,'; 
		                    
		                    if(empty($v2produce->when)) $v2produce->when = 0;
		                    
		                    $v2producesql .= " '".$v2produce->when."',";
		                } */

          				
        				
                        $v2producesql =substr($v2producesql,0,-1);
                        
                        $v2producesql .= " where e_oid = '".$v2produce->e_oid."'";
                        
                        if(!empty($debug))
                        {
                            echo $v2producesql."\n";
                        }else{
                            sql_execute($v2producesql);
                        }
                        unset($key);
                        unset($value);

                        /**
                         * pi_selects
                         */
                        if(isset($v2produce->pi_selects))
                        {
	                        if(!empty($v2produce->pi_selects))
	                        {
	                        	pi_selects_operate($v2produce->pi_selects,$debug);
	                        }
                        }
                        
                        /**
                         * v2_purchaseItems
                         */
                        if(isset($v2produce->v2_purchaseItems))
                        {
                        	if(!empty($v2produce->v2_purchaseItems))
                        	{
                        		v2_purchaseItems_operate($v2produce->v2_purchaseItems,$debug);
                        	}
                        }
                        
        				
        			}else{//add
			            $v2producefield = '';
			            $v2producevalue = '';
			            
			            foreach($obj->v2_produces as $key => $value)
			            {
			                if(!in_array($key,array('pi_selects','v2_pUsers','v2_purchaseItems','v2_events','v2_tracks','v2_produces','v2_notes','_exist','when','deadline')))
			                {
			                    $v2producefield .='`'.$key.'`,';
			
			                    if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date'))) $value = SimpleDate::toStamp($value);
			                        
			                    $v2producevalue .= " '".addslashes($value)."',";                      
			                
			                }
			
			                if(in_array($key,array('when','deadline')))
			                {
			                    $v2producefield .='`$key`,'; 
			                    
			                    if(empty($obj->$key))
			                    {
			                    	$obj->$key = 0;
			                    	$v2producevalue .= " '".$obj->$key."',";
			                    	
			                    }else{
			                        $v2producevalue .= " '".SimpleDate::toStamp($obj->$key)."',";	
			                    }
			                    
			                    
			                }               
			            
			            
			            }
			
			            $v2producefield = substr($v2producefield,0,-1);
			            $v2producevalue = substr($v2producevalue,0,-1);
			
			            $v2producesql = "insert into ns_produce(`e_oid`,".$v2producefield.") values(NULL,".$v2producevalue.")";  
			             
			            if(!empty($debug))
			            {
			                echo $v2producesql."\n";         
			            }else{
			                sql_execute($v2producesql);
			            } 
			            
			            unset($key);
			            unset($value);

			            /**
			             * pi_selects
			             */
        			                        /**
                         * pi_selects
                         */
                        if(isset($v2produce->pi_selects))
                        {
                            if(!empty($v2produce->pi_selects))
                            {
                                pi_selects_operate($v2produce->pi_selects,$debug);
                            }
                        }
                        
                        /**
                         * v2_purchaseItems
                         */
                        if(isset($v2produce->v2_purchaseItems))
                        {
                            if(!empty($v2produce->v2_purchaseItems))
                            {
                                v2_purchaseItems_operate($v2produce->v2_purchaseItems,$debug);
                            }
                        }
        				
        			}//end add
        		}
        	}
        	
        	
        }//exist v2_produces ,end v2_produces operate
 		
 		
 	}//obj not null
 	
 }//end pdoupdate    

  /**
   * produceremove
   *  $tablename,$condition,$cvalue,$debug=''
   */
 
  function produceremove($tablename,$condition,$cvalue,$debug='')
  {
  	$delsql = " delete from ns_".$tablename."  where `".$condition."` = '".$cvalue."' ";
  	
  	if(!empty($debug))
  	{
  		echo $delsql."\n";
  	}else{
  		sql_execute($delsql);
  	}
  	
  }
 

?>